package com.xw.db137.controller;

import com.xw.db137.common.JwtUtils;
import com.xw.db137.common.R;
import com.xw.db137.entity.Menu;
import com.xw.db137.service.MenuService;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: xw
 * @CreateTime: 2024/12/24
 * @Description: 用户
 * @Version: 1.0
 */
@RestController
@CrossOrigin(origins = "*")
public class MenuController {
    @Autowired
    private MenuService menuService;
    /**
     * 根据登录用户的id返回用户的菜单集合数据
     * @return
     */
    @GetMapping("/getMenuById")
    public R getMenuById(@RequestHeader("token") String  token) {
        System.out.println(token);
        //解析token，获取用户信息
        Integer id = JwtUtils.getJwtToken(token,"userid");
        System.out.println(id);
        List<Menu> menus = menuService.selectMenuByUserId(id);
        return new R().addData("menus", menus);
    }
    @GetMapping("/menuList")
    public R menuList() {
        List<Menu> list = menuService.list();
        return new R().addData("list", list);
    }
    @GetMapping("/deleteMenu")
    public R deleteMenu(@RequestParam String id) {
        menuService.deleteMenu(id);
        return new R().addData("id",id);
    }
    @PostMapping("/updateMenu")
    public int updateMenu(@RequestBody Menu menu) {
        return menuService.updateMenuById(menu);
    }
    @PostMapping("/addMenu/{label}/{href}/{imgicon}/{parentid}")
    public int addMenu(@PathVariable("label")String label,@PathVariable("href")String href,@PathVariable("imgicon")String imgicon,@PathVariable("parentid")Integer parentid) {
        Menu menu = new Menu();
        menu.setLabel(label);
        menu.setHref(href);
        menu.setImgicon(imgicon);
        menu.setParentld(parentid);
        return menuService.insertMenu(menu);
    }
    @GetMapping("selectMenuById/{id}")
    public R selectMenuById(@PathVariable("id") String id) {
        Menu byId = this.menuService.getById(id);
        return new R().addData("menu", byId);
    }
}
